package demoMod.stsconsole.commands;

import demoMod.stsconsole.ConsoleEntry;
import demoMod.stsconsole.dungeons.AbstractDungeon;
import demoMod.stsconsole.enums.ErrType;
import demoMod.stsconsole.helpers.LocalizedStrings;
import demoMod.stsconsole.interfaces.PreRestSubscriber;
import demoMod.stsconsole.rooms.AbstractRoom;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Scanner;

public class Rest extends AbstractCommand {
    private int healAmount;

    @Override
    public void execute(String[] args, Scanner sc) {
        if (args.length != 0) {
            this.errMsg(ErrType.WRONG_ARGUMENT);
            return;
        }
        if (AbstractDungeon.currDungeon.currMapNode.room.phase == AbstractRoom.RoomPhase.COMPLETED) {
            this.errMsg(ErrType.COMMAND_NOT_AVAILABLE);
            return;
        }
        List<String> cmdStrings = LocalizedStrings.getCommandStrings("Rest");
        this.healAmount = (int) Math.ceil(AbstractDungeon.player.getMaxHealth() * 0.3);
        ConsoleEntry.triggerSubscribers(PreRestSubscriber.class,
                "onRest",
                new Class[]{int.class},
                new Object[]{healAmount},
                (method, params, subscriber) -> {
                    try {
                        params[0] = method.invoke(subscriber, params);
                        healAmount = (int) params[0];
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
        );
        AbstractDungeon.player.heal(this.healAmount);
        System.out.printf(cmdStrings.get(0), AbstractDungeon.player.getCurrentHealth(), AbstractDungeon.player.getMaxHealth());
        System.out.println();
        AbstractDungeon.currDungeon.currMapNode.room.phase = AbstractRoom.RoomPhase.COMPLETED;
    }

    @Override
    public boolean requiredPrompt(String currentPrompt) {
        return currentPrompt.contains("Campfire");
    }
}
